/* 
 * File:   message_parser.h
 * Author: james
 *
 * Created on 2011年3月31日, 下午1:45
 */

#ifndef MESSAGE_PARSER_H
#define	MESSAGE_PARSER_H

#include "message.h"
#include "log.h"
namespace edm
{
    namespace network
    {
        class message_parser_t
        {
        public:
            static int parse_header(void* buffer, unsigned int len, message_header_t& hdr)
            {
                logtrace((MYNET, "message_parser_t::parse_header begin ..."));
                if(len < sizeof(message_header_t))
                {
                    logerror((MYNET, "message_parser_t::parse_header buffer size too small."));
                    return -1;
                }
                
                //!TODO:: ntoh transfer
                memcpy(&hdr, buffer, sizeof(message_header_t));
                if(hdr.magic != MESG_HDR_MAGIC)
                {
                    logerror((MYNET, "message_parser_t::parse_header magic not right."));
                    return -1;                    
                }
                
                //! reset body length
                hdr.len = hdr.len - sizeof(message_header_t);
                if(hdr.len < 0)
                {
                    logerror((MYNET, "message_parser_t::parse_header body len < 0."));
                    return -1;                    
                }    
                
                logtrace((MYNET, "message_parser_t::parse_header begin end ok."));
                return 0;
            }
        };
    }
}

#endif	/* MESSAGE_PARSER_H */

